// pages/person/viillageList/index.js
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
import * as Storage from '../../../../utils/storage';
const $api = require('../../../../request/api').API;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    modelList: [],
    showModelList: false,
    pageNo: 1,
    pageSize: 10,
    totalPage: '',
    empty: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  getData() {
    const { pageNo, pageSize } = this.data;
    const params = {
      pageNo,
      pageSize
    };
    $api.getVillageListPage(params).then(res => {
      const { list, totalPage } = res.data.data;
      if (pageNo > 1) {
        this.setData({
          list: this.data.list.concat(list)
        });
      } else {
        this.setData({
          list,
          empty: list.length > 0 ? false : true,
          totalPage
        });
      }
    });
  },
  showModelListPopup(e) {
    this.villageId = e.currentTarget.dataset.villageid;
    $api.getServerModelList({ villageId: this.villageId }).then(res => {
      this.setData({
        modelList: res.data.data,
        showModelList: true
      });
    });
  },
  hideModelListPopup() {
    this.setData({
      showModelList: false
    });
  },
  selectModel(e) {
    const { modelId, modelName } = e.detail.value;
    // console.log(this.villageId,id,name)
    const params = {
      villageId: this.villageId,
      serverModelId: modelId,
      userId: wx.getStorageSync(Storage.USER_ID)
    };
    this.hideModelListPopup();
    $api.userStack(params).then(res => {
      Toast.success('生成成功');
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let { pageNo, totalPage } = this.data;
    if (pageNo == totalPage) return;
    this.setData({
      pageNo: ++pageNo
    });
    this.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
